package com.cat.dataStructure03;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/contest/weekly-contest-475/problems/minimum-distance-between-three-equal-elements-i/https://leetcode.cn/contest/weekly-contest-475/problems/minimum-distance-between-three-equal-elements-i/
 * @create 2025/11/9 10:31
 * @since JDK17
 */

public class Solution27 {
    public int minimumDistance(int[] nums) {
        int ans = Integer.MAX_VALUE, n = nums.length;
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.computeIfAbsent(nums[i], x -> new ArrayList<>()).add(i);
        }
        for (var p : map.values()) {
            int min = Integer.MAX_VALUE;
            for (int i = 2; i < p.size(); i++) {
                min = Math.min((p.get(i) - p.get(i - 2)) * 2, min);
            }
            ans = Math.min(min, ans);
        }

        return ans == Integer.MAX_VALUE ? -1 : ans;
    }
}
